package com.gosophia.conferenceManagement.test.unit;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.scheme.entity.SchemeTransientStore;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.userManagement.entity.UserInfo;

@ContextConfiguration(locations = { "classpath:/applicationContext-test.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UTSchemeTransientStoreMapping extends
    AbstractTransactionalJUnit4SpringContextTests{

    @Autowired
    private SessionFactory sessionFactory;
    
    @Test
    @SuppressWarnings("unchecked")
    public void allClassMapping() throws Exception {
        Session session = sessionFactory.getCurrentSession();
        Map metadata = sessionFactory.getAllClassMetadata();
        for (Object o : metadata.values()) {
            EntityPersister persister = (EntityPersister) o;
            Object e = generateEntity(persister.getMappedClass(EntityMode.POJO));
            if(e==null) {
                continue;
            }
            session.save(e);
            session.flush();
            Serializable id = persister.getIdentifier(e, EntityMode.POJO);
            session.clear();
            session.get(persister.getMappedClass(EntityMode.POJO), id);
        }
    }

    @SuppressWarnings({ "unchecked" })
    private Object generateEntity(Class entityClass) {
        if (entityClass.equals(SchemeTransientStore.class)) {
            Session session = sessionFactory.getCurrentSession();
            SchemeTransientStore schemeTransientStore = new SchemeTransientStore();
            schemeTransientStore.setConferenceName("会议名称");
            schemeTransientStore.setCreatDate(new Date());
            schemeTransientStore.setDescription("描述");
            schemeTransientStore.setNextStep("下一环节名称");
            schemeTransientStore.setNote("备注");
            schemeTransientStore.setPlanEndDate(new Date());
            UserInfo commandPerson = new UserInfo();
            commandPerson.setUserName("小华");
            session.save(commandPerson);
            schemeTransientStore.setCommandPerson(commandPerson);
            
            UserInfo creator = new UserInfo();
            creator.setUserName("创建人");
            session.save(creator);
            schemeTransientStore.setCreator(creator);
            
            Document schemeDocument = new Document();
            schemeDocument.setDocumentName("文档名称");
            session.save(schemeDocument);
            schemeTransientStore.setSchemeDocument(schemeDocument);
            
            DataValue importance = new DataValue();
            importance.setDataValueDescliption("描述");
            importance.setValue("重要度");
            session.save(importance);
            schemeTransientStore.setImportance(importance);
            
            schemeTransientStore.setJobId(1L);
            return schemeTransientStore;
        } else{
            return null;
        }
    }
}
